﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class ChiTietMonAnDAO: DataProvider
    {
        public void ThemMonAn(ChiTietMonAnDTO ctma)
        {
            connect();
            string sql = "insert into ChiTietMonAn(mon_an_id,nvl_id,sl_mot_phan_an) values("+ctma.MONANID+","+ctma.NVLID+","+ctma.SOLUONGPHANAN+")";
            executeNonQuery(sql);
            disconnect();
        }

        public List<ChiTietMonAnDTO> GetChiTietMonAn(int mamonan)
        {
            connect();
            List<ChiTietMonAnDTO> lis = new List<ChiTietMonAnDTO>();
            string sql = "select * from ChiTietMonAn where mon_an_id=" + mamonan;
            SqlCommand cmd = new SqlCommand(sql, connection);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ChiTietMonAnDTO dto = new ChiTietMonAnDTO();
                dto.MONANID = reader.GetInt32(0);
                dto.NVLID = reader.GetInt32(1);
                float temp = float.Parse(reader.GetDouble(2).ToString());
                dto.SOLUONGPHANAN = temp;
                lis.Add(dto);
            }
            disconnect();
            return lis;
        }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            ChiTietMonAnDTO dto = new ChiTietMonAnDTO();
            dto.MONANID = int.Parse(dt.Rows[i][0].ToString());
            dto.NVLID = int.Parse(dt.Rows[i][1].ToString());
            dto.SOLUONGPHANAN = int.Parse(dt.Rows[i][2].ToString());
            return (object)dto;
        }

        public void CapNhatChiTietMonAn(ChiTietMonAnDTO dto)
        {
            connect();
            string sql = "Update ChiTietMonAn set nvl_id=" + dto.NVLID + ", sl_mot_phan_an=" + dto.SOLUONGPHANAN + " Where mon_an_id=" + dto.MONANID + " and nvl_id=" + dto.NVLID;
            executeNonQuery(sql);
            disconnect();
        }

        public void XoaChiTietMonAn(int mamonan, int mannvl)
        {
            connect();
            string deleteCommand = "DELETE FROM ChiTietMonAn WHERE mon_an_id = " + mamonan + " and nvl_id=" + mannvl;
            executeNonQuery(deleteCommand);
            disconnect();
        }
    }
}
